const axios = require('axios');
const vscode = require('vscode');
const md5 = require('md5');
const appid  = vscode.workspace.getConfiguration('aclfy_translate').appid;
const secret = vscode.workspace.getConfiguration('aclfy_translate').secret;

function translate(text, lang) {
    const salt = Math.floor(Math.random() * 1000);
    const sign = md5(appid + text + salt + secret);
    console.log(appid, secret, 'sign', lang);
    const url = 'https://fanyi-api.baidu.com/api/trans/vip/translate';
    let params = {
        q: text,
        appid: appid,
        salt: salt,
        sign: sign,
        from: lang ? 'zh' : 'en',
        to: lang ? 'en' : 'zh'
    };
    return axios.get(url, {params}).then(res => {
        return res.data;
    });
}
module.exports = {
    translate
};